<template>
  <div class="login-container">
    <div class="login_box">
      <el-form v-show="loginFormShow" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">关键信息抽取平台</h3>
        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model.trim="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model.trim="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
          <el-form-item prop="usertype" style="text-align: center;">
            <el-radio-group v-model="loginForm.usertype">
              <el-radio :label="1">管理员</el-radio>
              <el-radio :label="2">普通用户</el-radio>
              <el-radio :label="3">专业用户</el-radio>

            </el-radio-group>
          </el-form-item>
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
      </el-form>
      <el-form v-show="registerFormShow" ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" auto-complete="on" label-position="left">
        <el-form-item prop="newUsername">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model.trim="registerForm.newUsername"
            placeholder="新用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="newPassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="newPassword"
            v-model.trim="registerForm.newPassword"
            :type="passwordType"
            placeholder="新密码"
            name="newPassword"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="confirmPassword"
            v-model.trim="registerForm.confirmPassword"
            :type="passwordType"
            placeholder="再次输入密码"
            name="confirmPassword"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>

          <el-form-item prop="usertype" style="text-align: center;">
            <el-radio-group v-model="registerForm.usertype">
              <el-radio :label="2">普通用户</el-radio>
              <el-radio :label="3">专业用户</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleRegister">注册</el-button>
      </el-form>
      <el-button v-show="loginFormShow" type="text" class="register" @click.native.prevent="showForm1">注册</el-button>
      <el-button v-show="registerFormShow" type="text" class="change-password" @click.native.prevent="showForm1">返回</el-button>
      <el-form v-show="changePasswordFormShow" ref="changePasswordForm" :model="changePasswordForm" :rules="changePasswordRules" class="change-password-form" auto-complete="on" label-position="left">

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model.trim="changePasswordForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="oldPassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="oldPassword"
            v-model.trim="changePasswordForm.oldPassword"
            :type="passwordType"
            placeholder="原密码"
            name="oldPassword"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-form-item prop="newPassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="newPassword"
            v-model.trim="changePasswordForm.newPassword"
            :type="passwordType"
            placeholder="新密码"
            name="newPassword"
            tabindex="3"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="confirmPassword"
            v-model.trim="changePasswordForm.confirmPassword"
            :type="passwordType"
            placeholder="确认密码"
            name="confirmPassword"
            tabindex="4"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleChangePassword">修改密码</el-button>
      </el-form>
      <el-button v-show="loginFormShow" type="text" style="text-align: left" class="change-password" @click.native.prevent="showForm">修改密码</el-button>
      <el-button v-show="changePasswordFormShow" type="text" class="change-password" @click.native.prevent="showForm">返回</el-button>
    </div>
  </div>
</template>

<script>

import { Message } from 'element-ui'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 0) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不少于6个字符'))
      } else {
        callback()
      }
    }
    const validateNewPassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不少于6个字符'))
      } else if (value === this.changePasswordForm.oldPassword) {
        callback(new Error('新密码不能与原密码一致'))
      } else if (this.changePasswordForm.confirmPassword.length !== 0 && value !== this.changePasswordForm.confirmPassword) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    const validateRegisterPassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不少于6个字符'))
      } else {
        callback()
      }
    }
    const confirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.changePasswordForm.newPassword) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }

    const confirmRegisterPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.newPassword) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
        usertype: 1
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      changePasswordForm: {
        username: '',
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      changePasswordRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        oldPassword: [{ required: true, trigger: 'blur', validator: validatePassword }],
        newPassword: [{ required: true, trigger: 'blur', validator: validateNewPassword }],
        confirmPassword: [{ required: true, trigger: 'blur', validator: confirmPassword }]
      },
      registerForm: {
        newUsername: '',
        newPassword: '',
        confirmPassword: '',
        usertype: 2
      },
      registerRules: {
        newUsername: [{ required: true, trigger: 'blur', validator: validateUsername }],
        newPassword: [{ required: true, trigger: 'blur', validator: validateRegisterPassword }],
        confirmPassword: [{ required: true, trigger: 'blur', validator: confirmRegisterPassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      loginFormShow: true,
      changePasswordFormShow: false,
      registerFormShow: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      console.log('step1')
      console.log(this.loginForm)
      this.$refs.loginForm.validate(valid => {
        console.log('step2')
        if (valid) {
          console.log('step3')
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(res => {
            console.log(res)
            console.log('跳转失败1')
            this.loading = false
          })
        } else {
          console.log('跳转失败2')
          return false
        }
      })
    },
    handleChangePassword() {
      this.$refs.changePasswordForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/changePassword', this.changePasswordForm).then(res => {
            if (res.code === 1000) {
              Message({
                message: res.message || 'Success',
                type: 'success',
                duration: 2 * 1000
              })
              this.showForm()
            } else {
              Message({
                message: res.message || 'Error',
                type: 'error',
                duration: 2 * 1000
              })
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/register', this.registerForm).then(res => {
            console.log('注册成功！！')
            if (res.code === 1000) {
              Message({
                message: res.message || 'Success',
                type: 'success',
                duration: 2 * 1000
              })
              this.showForm1()
            } else {
              Message({
                message: res.message || 'Error',
                type: 'error',
                duration: 2 * 1000
              })
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    showForm() {
      if (this.loginFormShow) {
        this.loginFormShow = false
        this.changePasswordFormShow = true
      } else {
        this.loginFormShow = true
        this.changePasswordFormShow = false
      }
    },
    showForm1() {
      if (this.loginFormShow) {
        this.loginFormShow = false
        this.registerFormShow = true
      } else {
        this.loginFormShow = true
        this.registerFormShow = false
      }
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.5);
    background: rgba(255, 255, 255, 0.3);
    border-radius: 5px;
    color: #454545;
  }

}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-size:100% 100%;
  background-repeat: no-repeat;
  background-image: url("/static/1.jpg");
  //background-image: url("public/static/1.jpg");

  .login-form {
    position: relative;
    width: 520px;
    height: 390px;
    max-width: 100%;
    padding: 60px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .change-password-form {
    position: relative;
    width: 520px;
    height: 390px;
    max-width: 100%;
    padding: 20px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .register-form {
    position: relative;
    width: 520px;
    height: 390px;
    max-width: 100%;
    padding: 20px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 28px;
      color: $light_gray;
      margin: 0px auto 20px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .change-password {
    position: absolute;
    left: 35px;
    bottom: 0px;
  }
  .register {
    position: absolute;
    right: 35px;
    bottom: 0px;
  }
}
.login_box {
  width: 450px;
  height: 400px;
  /* background-color: #ffffff; */
  background: rgba(0, 0, 0, 0.7);
  //opacity: 0.85;
  filter: alpha(opacity=90);
  border-radius: 20px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
